<template>
  <div class="equipmentAdd">
    <div class="top">
      <div class="border"></div>
      <h3 v-if="deviceEdit=='deviceEdit'">{{equipmentAdd.edit}}</h3>
      <h3 v-else>{{equipmentAdd.add}}</h3>
    </div>
    <div class="main">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-position="top"
        class="demo-ruleForm equipmentForm"
      >
        <el-form-item :label="equipmentAdd.No" prop="No">
          <el-input v-model.trim="ruleForm.No" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item :label="equipmentAdd.name" prop="name">
          <el-input v-model.trim="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="cancelBtn boxShadowButton" size="small" @click="toRoomManagement">{{equipmentAdd.cancel}}</el-button>
          <el-button class="submitBtn boxShadowButton" size="small" @click="add('ruleForm')">{{equipmentAdd.enter}}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import bus from "../../../vuex/eventBus"; //引入eventBus,接收子界面传递的跳转指令,然后在父界面跳转,添加tab
export default {
  data() {
    return {
      equipmentAdd: this.$t("m.equipmentAdd"), //引入 lang 里面的字符串资源,
      ruleForm: {
        No: "",
        name: "",
        id:"",
      },
      roomNo: "",
      roomFloor: "",
      build: "",
      type: "", //设备类型
      deviceEdit:"",//不为空就是从修改页面跳转过来的
      acceptLanguage:'ja_JP',//数据请求传参需要传的语言  ja_JP----日文
    };
  },
  methods: {
    //返回
    toRoomManagement() {
      sessionStorage.removeItem('deviceType');//返回移除deviceType
      if(this.deviceEdit!=null && this.deviceEdit!=undefined && this.deviceEdit!=""){
        sessionStorage.removeItem('deviceEdit');//返回移除deviceEdit
        sessionStorage.removeItem('device_Info');//返回移除device_Info
      }
      if (this.type == "bed") {
        bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/BedpadManagement"});
      } else if (this.type == "personFeel") {
        bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/PeopleFeelManagement"});
      } else if (this.type == "camera") {
        bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/CameraManagement"});
      } else if (this.type == "indoor") {
        bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/IndoorTemperatureManagement"});
      } else if( this.type == "gateway") {
        bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/GatewayManagement"});
      }
    },

    //添加设备
    add(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          let param = {
            deviceNo: this.ruleForm.No,
            deviceName: this.ruleForm.name
          };
          //判断是不是修改  修改需要传id
          if(this.deviceEdit=="deviceEdit"){
            param.id=this.ruleForm.id
          }
          if (this.type == "bed") {
            //床垫
            let url="";
            if(this.deviceEdit=="deviceEdit"){
              //修改
              url="dmc/csunSleep/edit";
            }else{
              //新增
              url="dmc/csunSleep/add";
            }
            this.$postJson(url, param,this.acceptLanguage).then(res => {
              if (res.code == 200) {
                sessionStorage.removeItem('deviceType');
                if(this.deviceEdit!=null && this.deviceEdit!=undefined && this.deviceEdit!=""){
                  sessionStorage.removeItem('deviceEdit');//返回移除deviceEdit
                  sessionStorage.removeItem('device_Info');//返回移除device_Info
                }
                this.$message({message:res.message,type:"success"});
                bus.$emit("refreshBedList");//通知床垫列表刷新数据
                bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/BedpadManagement"});
              }else {
                this.$message({message:res.message,type:"error"});
              }
            });
          } else if (this.type == "personFeel") {
            //人感
            let url="";
            if(this.deviceEdit=="deviceEdit"){
              //修改
              //url="dmc/bodySensor/edit";
              //by guo 2020-4-22-17:30 修改   业务需求变更
              url="dmc/newBody/edit"
            }else{
              //新增
              // url="dmc/bodySensor/add";
              //by guo 2020-4-22-17:30 修改  业务需求变更
              url="dmc/newBody/add"
            }
            this.$postJson(url, param,this.acceptLanguage).then(res => {
              if (res.code == 200) {
                sessionStorage.removeItem('deviceType');
                if(this.deviceEdit!=null && this.deviceEdit!=undefined && this.deviceEdit!=""){
                  sessionStorage.removeItem('deviceEdit');//返回移除deviceEdit
                  sessionStorage.removeItem('device_Info');//返回移除device_Info
                }
                bus.$emit("refreshBodyList");//通知人感列表刷新数据
                this.$message({message:res.message,type:"success"});
                bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/PeopleFeelManagement"});
              }else{
                this.$message({message:res.message,type:"error"});
              }
            });
          } else if (this.type=="camera") {
            //摄像头
            let url="";
            if(this.deviceEdit=="deviceEdit"){
              //修改
              url="dmc/camera/edit";
            }else{
              //新增
              url="dmc/camera/add";
            }
            this.$postJson(url,param,this.acceptLanguage).then(res=>{
              if(res.code ==200) {
                sessionStorage.removeItem('deviceType');
                if(this.deviceEdit!=null && this.deviceEdit!=undefined && this.deviceEdit!=""){
                  sessionStorage.removeItem('deviceEdit');//返回移除deviceEdit
                  sessionStorage.removeItem('device_Info');//返回移除device_Info
                }
                bus.$emit("refreshCameraList");//通知摄像头列表刷新数据
                this.$message({message:res.message,type:"success"});
                bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/CameraManagement"});
              }else{
                this.$message({message:res.message,type:"error"});
              }
            })
          }else if (this.type=="indoor") {
            //温湿度传感器
            let url="";
            if(this.deviceEdit=="deviceEdit"){
              //修改
              url="dmc/humidistat/edit";
            }else{
              //新增
              url="dmc/humidistat/add";
            }
            this.$postJson(url,param,this.acceptLanguage).then(res=>{
              if(res.code ==200) {
                sessionStorage.removeItem('deviceType');
                if(this.deviceEdit!=null && this.deviceEdit!=undefined && this.deviceEdit!=""){
                  sessionStorage.removeItem('deviceEdit');//返回移除deviceEdit
                  sessionStorage.removeItem('device_Info');//返回移除device_Info
                }
                bus.$emit("refreshIndoorList");//通知温湿度传感器列表刷新数据
                this.$message({message:res.message,type:"success"});
                bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/IndoorTemperatureManagement"});
              }else{
                this.$message({message:res.message,type:"error"});
              }
            })
          }else if (this.type=="gateway") {
            //网关管理
            let url="";
            if(this.deviceEdit=="deviceEdit"){
              //修改
              url="dmc/gateway/edit";
            }else{
              //新增
              url="dmc/gateway/add";
            }
            this.$postJson(url,param,this.acceptLanguage).then(res=>{
              console.log(res);
              if(res.code==200){
                sessionStorage.removeItem('deviceType');
                if(this.deviceEdit!=null && this.deviceEdit!=undefined && this.deviceEdit!=""){
                  sessionStorage.removeItem('deviceEdit');//返回移除deviceEdit
                  sessionStorage.removeItem('device_Info');//返回移除device_Info
                }
                bus.$emit("refreshGatewayList");//通知网关列表刷新数据
                this.$message({message:res.message,type:"success"});
                bus.$emit("homePage",{type:"remove",path:"/equipmentAdd",showpath:"/GatewayManagement"});
              }else { 
                this.$message({message:res.message,type:"error"});
              }
            })
          }
        }
      });
    }
  },
  computed: {
    rules() {
      return {
        No: [
          {
            required: true,
            message: this.$t("m.equipmentAdd.null"),
            trigger: "blur"
          },
          {
            // pattern: /^(?=.*?[a-z])(?=.*?[0-9])(?=.*?[_-])[a-z0-9_-]+$/gi,
            pattern:/^[a-zA-Z0-9-_]+$/gi,//修改了规则  使规则适用于5种添加的设备
            message: this.$t("m.equipmentAdd.rules1")
          },
          { min: 1, max: 35, message: this.$t("m.equipmentAdd.long") }
        ],
        name: [
          {
            required: true,
            message: this.$t("m.equipmentAdd.null"),
            trigger: "blur"
          },
          { min: 1, max: 35, message: this.$t("m.equipmentAdd.nameLong") }
        ]
      };
    }
  },
  mounted() {
    console.log(this.$route);
    this.type = sessionStorage.getItem('deviceType');
    if(sessionStorage.getItem('deviceEdit')!=undefined && sessionStorage.getItem('deviceEdit')!=null && sessionStorage.getItem('deviceEdit')!=""){
      this.deviceEdit= sessionStorage.getItem('deviceEdit');
      this.device_Info=JSON.parse(sessionStorage.getItem('device_Info'));
      this.ruleForm.No=this.device_Info.deviceNo;
      this.ruleForm.name=this.device_Info.deviceName;
      this.ruleForm.id=this.device_Info.id;
    } 
    console.log(this.deviceEdit);
  }
};
</script>
<style lang="scss" scoped>
.equipmentAdd {
  /deep/ .equipmentForm .el-input{
    width: 350px;
  }
  /deep/ input {
    width: 350px;
    border: none !important;
    border-bottom: 2px solid #cccccc !important;
    border-radius: 1px;
    background: #f6f6f6 !important;
  }

  .top {
    display: flex;
    align-items: center;
    .border {
      width: 10px;
      height: 30px;
      background: black;
      margin-right: 10px;
    }
  }
  .main {
    margin-left: 20px;
    margin-top: 20px;
    p {
      color: #ff0000;
      margin-top: 10px;
    }
  }
  .cancelBtn {
    background: #dddddd;
    color: #010101;
  }
  .submitBtn {
    background: #E6A23C;
    color: #ffffff;
  }
}
/* 修改element-ui 表格label的样式 */
/deep/ .equipmentForm .el-form-item__label {
  color: #0000ff !important;
}
</style>